<?php
class Admin_BetController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->_helper->acl->allow('user');
    }
    public function indexAction()
    {
    	$message = $this->_helper->FlashMessenger->getMessages();
	    if($message){
	    	$this->view->message = $message[0];
	    }
		$this->view->title = 'View all bets';
    	if($input = $this->_checkInput('page')){
	    	$bet = new Application_Model_Bet();
	    	$request = $this->getRequest();
	    	$this->view->bets = $bet->getAll(true, $input->page, false);
    	}
    	else{
	    	throw new Zend_Controller_Action_Exception('Invalid input');
	    }
    }
	public function editAction(){
		$this->view->title = 'Edit bet';
		$this->view->headTitle($this->view->title, 'PREPEND');
		if($input = $this->_checkInput('id', 'bet')){
			$bet = new Application_Model_Bet($input->id);
			$form = new Application_Form_AdminBet();
			$form->populate($bet->getRow());
	    	$request = $this->getRequest();
	    	if($request->isPost()){
    	    	if($form->isValid($request->getPost())){
    	    		$bet->fill($form->getValues());
    	    		$bet->recountBet();
     	    		$messenger = $this->_helper->FlashMessenger;
					$messenger->addMessage('Bet edited');
					$this->_helper->redirector(null, 'bet', 'admin');
    	    	}    		    		
   			}
   			$this->view->form = $form;							
	    }
	    else{
	    	throw new Zend_Controller_Action_Exception('Invalid input');
	    }	
	}
	public function addAction(){
		$this->view->title = 'Add bet';
		$this->view->headTitle($this->view->title, 'PREPEND');
		$form = new Application_Form_AdminBet('add');
	    $form->fillForm();
		$request = $this->getRequest();
	    if($request->isPost()){
    	   	if($form->isValid($request->getPost())){
    	   		$bet = new Application_Model_Bet();
    	   		$bet->fill($form->getValues());
    	   		$bet->save();
    	   		$messenger = $this->_helper->FlashMessenger;
				$messenger->addMessage('Bet added');
				$this->_helper->redirector(null, 'bet', 'admin');
    	   	}
	    }
	    $this->view->form = $form;		
	}
	public function deleteAction(){
		$this->_helper->viewRenderer->setNoRender();
		if($input = $this->_checkInput('id', 'bet')){
			$bet = new Application_Model_Bet($input->id);
			$bet->delete();
			$messenger = $this->_helper->FlashMessenger;
			$messenger->addMessage('Bet deleted');
			$this->_helper->redirector(null, 'bet', 'admin');
		}
		else{
			throw new Zend_Controller_Action_Exception('Invalid input');
		}		
	}
	private function _checkInput($param, $field = null){
		$validatorsSet = array();
		$validatorsSet[] = 'Int';
		$validatorsSet[] = new Zend_Validate_GreaterThan(0);
		$validatorsSet[] = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::ALL);
		if($field){
			$validatorsSet[] = new Zend_Validate_Db_RecordExists(array('table' => $field, 'field' => $param));
		}
    	$validators = array($param => $validatorsSet);
	    $input = new Zend_Filter_Input(null, $validators);
	    $input->setData($this->getRequest()->getParams());
	    if($input->isValid()){
	    	return $input;
	    }
	    else{
	    	return false;
	    }		
	}
}